package com.ebt.m.customer.fakeDialog.util;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.ebt.m.customer.fakeDialog.view.FakeDialogView;
import com.ebt.m.customer.fakeDialog.view.FakeDialogView.ChildModel;
import com.ebt.m.customer.fakeDialog.view.FakeDialogView.OnFakeDialogClickListener;
import java.util.ArrayList;

/**
 * Created by Rick.Wang on 2016/3/11.
 */
public class FakeDialogUtil {

    private static final String TAG = "FakeDialog";

    public static void toggleFakeDialog(Activity activity, final View foregroundView, ArrayList<ChildModel> childrenModels,
    									int[] fakeDialogViewMargins, boolean isAnim, final FakeDialogView.OnFakeDialogClickListener fakeDialogClickListener){
        if(isFakeDialogShowing(foregroundView)){
            removeFakeDialogForView(foregroundView);
        }else{
            addFakeDialogForView(activity, foregroundView, childrenModels, fakeDialogViewMargins, isAnim, fakeDialogClickListener);
        }
    }

    public static View addFakeDialogForView(Activity activity, final View foregroundView, ArrayList<ChildModel> childrenModels,
                                            int[] fakeDialogViewMargins, final boolean anim, final OnFakeDialogClickListener fakeDialogClickListener){
        ViewGroup viewGroup = (ViewGroup)foregroundView.getParent();

        int foregroundViewIndex = getForegroundViewIndexForFakeDialogView(foregroundView);

        if(foregroundViewIndex > -1){
        	
        	final FakeDialogView fakeDialogView = new FakeDialogView(activity);
        	fakeDialogView.setVisibility(View.INVISIBLE);
        	fakeDialogView.addChildrenViews(foregroundView, childrenModels, fakeDialogClickListener);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            //null
            if(fakeDialogViewMargins != null){
            	lp.leftMargin = fakeDialogViewMargins[0];
            	lp.topMargin = fakeDialogViewMargins[1];
            	lp.rightMargin = fakeDialogViewMargins[2];
            	lp.bottomMargin = fakeDialogViewMargins[3];
            }
            fakeDialogView.setLayoutParams(lp);
            viewGroup.addView(fakeDialogView, foregroundViewIndex);
            //TODO
            fakeDialogView.post(new Runnable() {
				@Override
				public void run() {
					fakeDialogView.show(anim);
				}
			});
            return fakeDialogView;
        }
        
        return null;
    }

    public static boolean isFakeDialogShowing(View foregroundView){
        ViewGroup viewGroup = (ViewGroup)foregroundView.getParent();

        int foregroundViewIndex = getForegroundViewIndexForFakeDialogView(foregroundView);
        
        if(foregroundViewIndex > 0){
            View fakeDialogView = viewGroup.getChildAt(foregroundViewIndex - 1);
            return (fakeDialogView instanceof FakeDialogView);
        }
        return false;
    }

    /**
     *
     * @param foregroundView
     */
    public static void removeFakeDialogForView(View foregroundView){
        final ViewGroup viewGroup = (ViewGroup)foregroundView.getParent();

        int foregroundViewIndex = getForegroundViewIndexForFakeDialogView(foregroundView);
        if(foregroundViewIndex > 0){
            final View fakeDialogView = viewGroup.getChildAt(foregroundViewIndex - 1);
            if(fakeDialogView instanceof FakeDialogView){
            	((FakeDialogView) fakeDialogView).dismiss();
            }
        }
    }

    public static int getForegroundViewIndexForFakeDialogView(View childView){
        ViewGroup viewGroup = (ViewGroup)childView.getParent();
        if(viewGroup != null && viewGroup instanceof RelativeLayout){
            int childCount = viewGroup.getChildCount();
            for(int i = 0; i < childCount; i++){
                if(viewGroup.getChildAt(i) == childView){
                    return i;
                }
            }
        }
        Log.d(TAG, "addFakeDialogForView viewGroup not instanceof RelativeLayout");
        return -1;
    }

}